//
//  SINAStuActivityDesViewController.m
//  sinashebao
//
//  Created by  androidlongs on 2018/1/23.
//  Copyright © 2018年  androidlongs. All rights reserved.
//

#import "SINAStuActivityDesViewController.h"
#import "SINACircularBeadUtils.h"
#import "SINAStuActivityTargetModel.h"
#import "SINAStuActivityImpressModel.h"
#import "SINAUiviewClickLiserner.h"
#import "SINAStuActivityLeaveWordViewController.h"
#import "SINAStuActivityAddTargetViewController.h"
#import "SINAStuActivityAddImpressionViewController.h"
#import "SINACalendarUtils.h"
//#import "SINASINAStuActivityAddTargetLevelViewController.h"
#import "SINAStuActivityAddTargetThreeViewController.h"






@interface SINAStuActivityDesViewController ()

@property(nonatomic,weak) IBOutlet UIScrollView *scrollView;


//条目4中活动显示标题
@property (weak, nonatomic) IBOutlet UILabel *activityTitleLabel4;
//条目4中 消息显示
@property (weak, nonatomic) IBOutlet UIImageView *activityMsgImageView;
//条目4中 消息上层覆盖
@property (weak, nonatomic) IBOutlet UIView *activityMsgUiView;
//条目4中 组队人头像
@property (weak, nonatomic) IBOutlet UIImageView *otherUserImageView;
//条目4中 组队人姓名
@property (weak, nonatomic) IBOutlet UILabel *otherUserNameLabel;
//条目4中 组队模式文字
@property (weak, nonatomic) IBOutlet UILabel *activityFlagLabel4;
//条目4中 组队完成按钮
@property (weak, nonatomic) IBOutlet UIButton *activityFinishButton;
//条目4中显示活动时间
@property (weak, nonatomic) IBOutlet UILabel *activityTimeLabel4;
//条目4中显示活动地址
@property (weak, nonatomic) IBOutlet UILabel *activityLocationLabel4;
//条目4中 组队标识
@property (weak, nonatomic) IBOutlet UIImageView *activityTeamFlagImageview4;
@property (weak, nonatomic) IBOutlet UILabel *activityTeamFlagLabel;
//条目2 小目标相关
@property (weak, nonatomic) IBOutlet UIView *itemView2;
//条目2中标题
@property (weak, nonatomic) IBOutlet UILabel *titleLabel2;
//条目2中填写提示
@property (weak, nonatomic) IBOutlet UILabel *titleflagLabel2;
//条目2中填写小图标
@property (weak, nonatomic) IBOutlet UIImageView *inputImageView2;
//条目2中标题分割线
@property (weak, nonatomic) IBOutlet UIView *diviView2;
//条目2中小目标
@property (weak, nonatomic) IBOutlet UIView *targetUiview;
@property (weak, nonatomic) IBOutlet UIView *addTargetClickView;



//条目三 随笔 相关
@property (weak, nonatomic) IBOutlet UIView *itemView3;
//条目3中标题
@property (weak, nonatomic) IBOutlet UILabel *titleLabel3;
//条目3中填写提示
@property (weak, nonatomic) IBOutlet UILabel *titleflagLabel3;
//条目3中填写小图标
@property (weak, nonatomic) IBOutlet UIImageView *inputImageView3;
//条目3中标题分割线
@property (weak, nonatomic) IBOutlet UIView *diviView3;
//条目3中 随笔 内容
@property (weak, nonatomic) IBOutlet UIView *impressionUiview;
@property (weak, nonatomic) IBOutlet UIView *addImpressionClickView;


@property (weak, nonatomic) IBOutlet UIView *itemView1;


@property(nonatomic,strong) NSMutableArray *teamTargetArray;
@property(nonatomic,strong) NSMutableArray *teamImpressionArray;



@end

@implementation SINAStuActivityDesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"活动详情";
    
    self.teamTargetArray=self.model.teamTargetArray;
    self.teamImpressionArray=self.model.teamImpressionArray;
    if(self.teamImpressionArray==nil){
        self.teamImpressionArray=[[NSMutableArray alloc]init];
    }
    if(self.teamTargetArray==nil){
        self.teamTargetArray=[[NSMutableArray alloc]init];
    }
    
    if ([self.model.itemStatue integerValue]==2) {
        //组队活动已完成
        [self hideTarAndImpressAddFunction];
    }
    
    
    [self initViewFunction];
    [self initDataFunction];
}

//隐藏编辑功能
-(void)hideTarAndImpressAddFunction{
    self.activityFinishButton.hidden=YES;
    self.titleflagLabel2.hidden = YES;
    self.titleflagLabel3.hidden = YES;
    self.inputImageView2.hidden=YES;
    self.inputImageView3.hidden=YES;
}
-(void)initDataFunction{
    
    
    //用户组队信息设置
    //未组队成功  与 已组队成功 共同需要设置的选项
    self.activityTitleLabel4.text = self.model.activityTitle;
    self.activityTimeLabel4.text=[NSString stringWithFormat:@"%@-%@",[SINACalendarUtils getDateStringWithTimeStr:self.model.activityStartTime andFormat:@"HH:mm"],[SINACalendarUtils getDateStringWithTimeStr:self.model.activityStopTime andFormat:@"HH:mm"]];
    self.activityLocationLabel4.text = self.model.activityLocation;
    
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:16],};
    
    NSString *str =self.model.activityTitle;
    CGSize textSize = [str boundingRectWithSize:CGSizeMake(mainW, 21) options:NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;;
    
    if (textSize.width>mainW/3*2) {
        textSize.width = mainW/3*2;
    }
    self.activityTitleLabel4.frame=CGRectMake(self.activityTitleLabel4.frame.origin.x, self.activityTitleLabel4.frame.origin.y, textSize.width, 21);
    
    
    //组队成功查看详情
    if([self.model.itemStatue integerValue]==1){
        [self.otherUserImageView sd_setImageWithURL:[NSURL URLWithString:self.model.activityUserImg] placeholderImage:[UIImage imageNamed:@"默认头像"]];
        self.otherUserNameLabel.text = self.model.activityUserName;
        
        self.activityMsgUiView.frame=CGRectMake(CGRectGetMaxX(self.activityTitleLabel4.frame)+10, self.activityMsgUiView.frame.origin.y, self.activityMsgUiView.frame.size.width, self.activityMsgUiView.frame.size.height);
        
    }else if([self.model.itemStatue integerValue]==0){
        //未组队成功查看详情
        [self hideTeamInformationFunction];
        
    }else {
        if(self.model.activityUserName!=nil&&self.model.activityUserName.length>0&&![self.model.activityUserName isEqualToString:@"(null)"]){
            [self.otherUserImageView sd_setImageWithURL:[NSURL URLWithString:self.model.activityUserImg] placeholderImage:[UIImage imageNamed:@"默认头像"]];
            self.otherUserNameLabel.text = self.model.activityUserName;
            
            self.activityMsgUiView.frame=CGRectMake(CGRectGetMaxX(self.activityTitleLabel4.frame)+10, self.activityMsgUiView.frame.origin.y, self.activityMsgUiView.frame.size.width, self.activityMsgUiView.frame.size.height);
            
        }
    }
    
    NSString *otherUserUid=self.model.activityUserUid;
    if (otherUserUid==nil||otherUserUid.length==0||[otherUserUid isEqualToString:@"(null)"]) {
        //未组队模式 不显示队友信息
        [self hideTeamInformationFunction];
    } else {
        //组队模式 显示队友信息
        
    }
    
    
    
    //设置完成按钮
    NSInteger activityStartTime = [self.model.activityStartTime integerValue];
    NSInteger activityEndTime = [self.model.activityStopTime integerValue];
    //当前时间
    NSInteger currentTime = [[SINACalendarUtils currentTimeStr] integerValue];
    
    if (currentTime<activityStartTime) {
        DLog(@"活动未开始");
        //活动未开始
        //活动未开始
        [self.activityFinishButton setBackgroundColor:[UIColor colorWithHexString:@"#878787"]];
        [self.activityFinishButton setTitle:@"完成" forState:UIControlStateNormal];
    }else if(currentTime>=activityStartTime&&currentTime<=(activityEndTime)){
        //中
        DLog(@"活动中");
        [self.activityFinishButton setBackgroundColor:baseSelectColor];
        [self.activityFinishButton setTitle:@"活动中" forState:UIControlStateNormal];
    }else if(currentTime>=activityEndTime&&currentTime<=(activityEndTime+30*60*1000)){
        //活动结束 以及结束后的半小时
        DLog(@"完成");
        [self.activityFinishButton setBackgroundColor:baseSelectColor];
        [self.activityFinishButton setTitle:@"完成" forState:UIControlStateNormal];
    }else{
        //超时
        [self.activityFinishButton setBackgroundColor:[UIColor colorWithHexString:@"#878787"]];
        [self.activityFinishButton setTitle:@"超时" forState:UIControlStateNormal];
        DLog(@"超时");
        [self hideTarAndImpressAddFunction];
        self.activityFinishButton.hidden=NO;
    }
    
    
    
    
    
    
    
    
    
}

//隐藏并更新组队信息
-(void) hideTeamInformationFunction{
    self.activityMsgUiView.hidden=YES;
    self.otherUserImageView.hidden=YES;
    self.otherUserNameLabel.hidden=YES;
    self.activityFlagLabel4.hidden=YES;
    self.activityTeamFlagImageview4.hidden=YES;
    self.activityTeamFlagLabel.hidden=YES;
    
    self.activityTimeLabel4.frame=CGRectMake(self.activityTimeLabel4.origin.x
                                             , CGRectGetMaxY(self.activityTitleLabel4.frame)+5, self.activityTimeLabel4.frame.size.width, self.activityTimeLabel4.frame.size.height);
    self.activityLocationLabel4.frame=CGRectMake(self.activityLocationLabel4.origin.x
                                                 , CGRectGetMaxY(self.activityTimeLabel4.frame)+3, CGRectGetWidth(self.itemView1.frame)-30, self.activityLocationLabel4.frame.size.height);
    self.itemView1.frame=CGRectMake(self.itemView1.frame.origin.x,self.itemView1.origin.y,self.itemView1.frame.size.width,92);
    
    self.activityFinishButton.frame = CGRectMake(self.activityFinishButton.frame.origin.x, CGRectGetHeight(self.itemView1.frame)/2-CGRectGetHeight(self.activityFinishButton.frame)/2, CGRectGetWidth(self.activityFinishButton.frame), CGRectGetHeight(self.activityFinishButton.frame));
    
}
-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.hidden=NO;
    [self loadingTarGetDataFunction];
}
-(void) initViewFunction{
    UIView *rightView =[[UIView alloc]initWithFrame:CGRectMake(0,0,100,50)];
    UIButton *rightButton =[[UIButton alloc]initWithFrame:CGRectMake(70,10,20,20)];
    [rightButton addTarget:self action:@selector(backFunction) forControlEvents:UIControlEventTouchUpInside];
    [rightView addSubview:rightButton];
    [rightButton setImage:[UIImage imageNamed:@"分享"] forState:UIControlStateNormal];
//    UIBarButtonItem *rightButtonView = [[UIBarButtonItem alloc]initWithCustomView:rightView ];
    //self.navigationItem.rightBarButtonItem=rightButtonView;
    
    
    self.scrollView.backgroundColor=[UIColor colorWithHexString:@"#eeeeee"];
    self.scrollView.frame = CGRectMake(0, 0, mainW,mainH-59);
    self.itemView1.frame = CGRectMake(15, 15, mainW-30, 135);
    [SINACircularBeadUtils makeCircularBead:1 andView:self.itemView1 andIsSetBackground:NO];
    
    self.activityTitleLabel4.frame = CGRectMake(15, 10, 40, 21);
    self.activityMsgUiView.frame=CGRectMake(CGRectGetMaxX(self.activityTitleLabel4.frame)+10, 0, 150, 30);
    self.activityMsgImageView.frame=CGRectMake(0, 12, 18, 17);
    
    self.otherUserImageView.frame=CGRectMake(15, CGRectGetMaxY(self.activityTitleLabel4.frame)+10, 40, 40);
    [SINACircularBeadUtils makeCircularView:self.otherUserImageView];
    
    self.otherUserNameLabel.frame=CGRectMake(CGRectGetMaxX(self.otherUserImageView.frame)+10, CGRectGetMaxY(self.activityTitleLabel4.frame)+12, 150, 21);
    self.activityFlagLabel4.frame=CGRectMake(CGRectGetMaxX(self.otherUserImageView.frame)+10, CGRectGetMaxY(self.otherUserNameLabel.frame), 150, 21);
    
    self.activityTimeLabel4.frame=CGRectMake(15, CGRectGetMaxY(self.otherUserImageView.frame)+5, 250, 20);
    self.activityLocationLabel4.frame=CGRectMake(15, CGRectGetMaxY(self.activityTimeLabel4.frame), 250, 21);
    
    self.activityFinishButton.frame=CGRectMake(CGRectGetWidth(self.itemView1.frame)-58-15, CGRectGetHeight(self.itemView1.frame)/2-13, 58, 26);
    [SINACircularBeadUtils makeCircularBead:1 andView:self.activityFinishButton andIsSetBackgroundColor:[UIColor colorWithHexString:@"FD8D1F"]];
    self.activityTeamFlagImageview4.frame=CGRectMake(CGRectGetWidth(self.itemView1.frame)-48, 12, 48, 16);
    self.activityTeamFlagLabel.frame=CGRectMake(CGRectGetWidth(self.itemView1.frame)-48, 12, 48, 16);
    
    //条目2相关
    [self initTargetUiViewFunction];
    
    [SINACircularBeadUtils makeCircularBead:1 andView:self.itemView2 andIsSetBackground:NO];
    
    self.titleLabel2.frame =CGRectMake(15, 0, CGRectGetWidth(self.itemView2.frame),45);
    self.titleflagLabel2.frame =CGRectMake(0, 0, CGRectGetWidth(self.itemView2.frame),45);
    self.inputImageView2.frame =CGRectMake(CGRectGetWidth(self.itemView2.frame)/2+70, CGRectGetHeight(self.titleLabel2.frame)/2-8, 16,16);
    self.addTargetClickView.frame =CGRectMake(0, 0, CGRectGetWidth(self.itemView2.frame),46);
    
    self.diviView2.frame =CGRectMake(15,CGRectGetMaxY(self.titleLabel2.frame),CGRectGetWidth(self.itemView2.frame)-30, 1);
    self.targetUiview.frame =CGRectMake(15,CGRectGetMaxY(self.diviView2.frame)+10,CGRectGetWidth(self.itemView2.frame)-30, CGRectGetMaxY(self.itemView2.frame)-15);
    
    
    
    self.targetUiview.backgroundColor =[UIColor clearColor];
    self.diviView2.backgroundColor=[UIColor colorWithHexString:@"#eeeeee"];
    
    //条目三中随笔内容
    [self initImpressUiViewFunction];
    
    [SINACircularBeadUtils makeCircularBead:1 andView:self.itemView3 andIsSetBackground:NO];
    self.titleLabel3.frame =CGRectMake(15, 0, CGRectGetWidth(self.itemView3.frame),45);
    self.titleflagLabel3.frame =CGRectMake(0, 0, CGRectGetWidth(self.itemView3.frame),45);
    self.inputImageView3.frame =CGRectMake(CGRectGetWidth(self.itemView3.frame)/2+70, CGRectGetHeight(self.titleLabel3.frame)/2-8, 16,16);
    self.diviView3.frame =CGRectMake(15,CGRectGetMaxY(self.titleLabel3.frame),CGRectGetWidth(self.itemView3.frame)-30, 1);
    
    // self.impressionUiview.backgroundColor =[UIColor clearColor];
    self.diviView3.backgroundColor=[UIColor colorWithHexString:@"#eeeeee"];
    self.addImpressionClickView.backgroundColor=[UIColor clearColor];
    self.addTargetClickView.backgroundColor=[UIColor clearColor];
    
    self.scrollView.contentSize=CGSizeMake(mainW, CGRectGetMaxY(self.itemView3.frame)+15);
    
    
    
    __weak typeof(self) weakSelf = self;
    //留言
    [[SINAUiviewClickLiserner alloc]viewClickFunction:self.activityMsgUiView andWithBlick:^(UIView *view) {
        SINAStuActivityLeaveWordViewController *addItemController = [[SINAStuActivityLeaveWordViewController alloc]initWithNibName:@"SINAStuActivityLeaveWordViewController" bundle:nil];
        addItemController.model =weakSelf.model;
        addItemController.hidesBottomBarWhenPushed=YES;
        weakSelf.navigationController.navigationBar.hidden=NO;
        [weakSelf.navigationController pushViewController:addItemController animated:YES];
    }];
    //添加小目标页面
    [[SINAUiviewClickLiserner alloc]viewClickFunction:self.targetUiview andWithBlick:^(UIView *view) {
        [self toaddTarPageFunction];
        
    }];
    [[SINAUiviewClickLiserner alloc]viewClickFunction:self.addTargetClickView andWithBlick:^(UIView *view) {
        [self toaddTarPageFunction];
        
    }];
    //添加随笔
    [[SINAUiviewClickLiserner alloc]viewClickFunction:self.addImpressionClickView andWithBlick:^(UIView *view) {
        
        //组队成功或者未组队
        if ([self.model.itemStatue integerValue]==1||[self.model.itemStatue integerValue]==0) {
            SINAStuActivityAddImpressionViewController *addItemController = [[SINAStuActivityAddImpressionViewController alloc]initWithNibName:@"SINAStuActivityAddImpressionViewController" bundle:nil];
            addItemController.hidesBottomBarWhenPushed=YES;
            addItemController.model = weakSelf.model;
            weakSelf.navigationController.navigationBar.hidden=NO;
            [weakSelf.navigationController pushViewController:addItemController animated:YES];
        }
        
    }];
}
-(void) toaddTarPageFunction{
    //组队成功或者未组队
    if ([self.model.itemStatue integerValue]==1||[self.model.itemStatue integerValue]==0) {
        /*
         *第一版
         * SINAStuActivityAddTargetViewController *addItemController = [[SINAStuActivityAddTargetViewController alloc]initWithNibName:@"SINAStuActivityAddTargetViewController" bundle:nil];
         **/
       
        
        //第二版
        // SINASINAStuActivityAddTargetLevelViewController *addItemController=[[SINASINAStuActivityAddTargetLevelViewController alloc]init];
        
        //第三版
        SINAStuActivityAddTargetThreeViewController
        *addItemController=[[SINAStuActivityAddTargetThreeViewController alloc]init];
       
        addItemController.activityMmodel = self.model;
        addItemController.targetArray=self.teamTargetArray;
        addItemController.hidesBottomBarWhenPushed=YES;
        self.navigationController.navigationBar.hidden=NO;
        
        [self.navigationController pushViewController:addItemController animated:YES];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
-(void) backFunction{
    [self.navigationController popViewControllerAnimated:YES];
}

//加载小目标
-(void)loadingTarGetDataFunction{
    DLog(@"加载小目标");

    [self startLoading];
    
    WeakSelf;
    [CXAFNHttpRequestUtils httpGetRequest:[NSString stringWithFormat:@"%@%@?organizeuuid=%@",baseUrl,userActivityTargetListUrl,self.model.activityUid] success:^(id responseData) {
        [weakSelf stopLoading];
        NSString *code = responseData[@"code"];
        
        if ([code isEqualToString:@"1000"]) {
            NSMutableArray *result = responseData[@"content"];
            if (result!=nil&&result.count>0) {
                [self.teamTargetArray removeAllObjects];
                for (NSInteger i=0; i<result.count; i++) {
                    NSDictionary *dic = result[i];
                    SINAStuActivityTargetModel *targetModel=[[SINAStuActivityTargetModel alloc]initWithDict:dic];
                    [self.teamTargetArray addObject:targetModel];
                }
                [self initTargetUiViewFunction];
                
            }else{
                [self.teamTargetArray removeAllObjects];
                [self initTargetUiViewFunction];
            }
            
        }else{
            [self.teamTargetArray removeAllObjects];
            
            [self initTargetUiViewFunction];
            //无数据
        }
        
        //加载随笔
        [self loadingImpressDataFunction];
        
    } failure:^(NSError *error, NSInteger errCode) {
        
        [weakSelf stopLoading];
        
        [self.teamTargetArray removeAllObjects];
        [self initTargetUiViewFunction];
        //加载随笔
        [self loadingImpressDataFunction];
    }];
}
//加载随笔
-(void)loadingImpressDataFunction{
    DLog(@"加载小随笔 ");
    
//    WeakSelf;
    [CXAFNHttpRequestUtils httpGetRequest:[NSString stringWithFormat:@"%@%@?organizeuuid=%@",baseUrl,userActivityImpressListUrl,self.model.activityUid] success:^(id responseData) {
        NSString *code = responseData[@"code"];
        if ([code isEqualToString:@"1000"]) {
            NSMutableArray *result = responseData[@"content"];
            if (result!=nil&&result.count>0) {
                
                [self.teamImpressionArray removeAllObjects];
                for (NSInteger i=0; i<result.count; i++) {
                    NSDictionary *dic = result[i];
                    SINAStuActivityImpressModel *targetModel=[[SINAStuActivityImpressModel alloc]initWithDict:dic];
                    [self.teamImpressionArray addObject:targetModel];
                }
                [self initImpressUiViewFunction];
            }else{
                [self.teamImpressionArray removeAllObjects];
                [self initImpressUiViewFunction];
            }
        }else{
            [self.teamImpressionArray removeAllObjects];
            
            NSString *message =responseData[@"message"];
            WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:message sureBtn:@"确定"
                                                                 cancleBtn:nil];
            [alertView showXLAlertView];
            
            [self initImpressUiViewFunction];
            //无数据
        }
        
    } failure:^(NSError *error, NSInteger errCode) {
        [self.teamImpressionArray removeAllObjects];
        [self initImpressUiViewFunction];
        //无数据
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"获取数据验证失败" sureBtn:@"确定"
                                                             cancleBtn:nil];
        [alertView showXLAlertView];
        
    }];
}

-(void) initTargetUiViewFunction{
    [self.targetUiview removeAllSubviews];
    NSInteger itemViewHeight = 46;
    if (self.teamTargetArray!=nil&&self.teamTargetArray.count>0) {
        itemViewHeight=  47+15+24*self.teamTargetArray.count;
        self.diviView2.hidden=NO;
        for (NSInteger i=0; i<self.teamTargetArray.count; i++) {
            SINAStuActivityTargetModel *targetModel =self.teamTargetArray[i];
            NSString *targetTitle =targetModel.title;
            if (targetTitle==nil||targetTitle.length==0||[targetTitle isEqualToString:@"(null)"]) {
                UILabel *taretLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, i*24, CGRectGetWidth(self.itemView2.frame), 21)];
                taretLabel.text=[NSString stringWithFormat:@"%ld.%@",i+1,@"***"];
                
                taretLabel.font=[UIFont systemFontOfSize:14];
                taretLabel.textColor=[UIColor colorWithHexString:@"#676767"];
                [self.targetUiview addSubview:taretLabel];
            }else{
                UILabel *taretLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, i*24, CGRectGetWidth(self.itemView2.frame), 21)];
                taretLabel.text=[NSString stringWithFormat:@"%ld、%@",i+1,targetTitle];
                taretLabel.font=[UIFont systemFontOfSize:14];
                taretLabel.textColor=[UIColor colorWithHexString:@"#676767"];
                [self.targetUiview addSubview:taretLabel];
            }
        }
    }else{
        self.diviView2.hidden=YES;
    }
    self.itemView2.frame = CGRectMake(15, CGRectGetMaxY(self.itemView1.frame)+15, mainW-30, itemViewHeight);
}

-(void) initImpressUiViewFunction{
    [self.impressionUiview removeAllSubviews];
    NSInteger total = 0;
    self.itemView3.frame = CGRectMake(15, CGRectGetMaxY(self.itemView2.frame)+15, mainW-30, 46);
    self.addImpressionClickView.frame =CGRectMake(CGRectGetWidth(self.itemView3.frame)/2+70, 0, 80,46);
    self.impressionUiview.backgroundColor=[UIColor clearColor];
    if (self.teamImpressionArray!=nil&&self.teamImpressionArray.count>0) {
        
        self.diviView3.hidden=NO;
        for (NSInteger i=0; i<self.teamImpressionArray.count; i++) {
            SINAStuActivityImpressModel *impressModel =self.teamImpressionArray[i];
            
            UIView *impressView=[[UIView alloc]initWithFrame:CGRectMake(0, total, CGRectGetWidth(self.itemView3.frame)-30, 21)];
            impressView.backgroundColor=[UIColor clearColor];
            
            //测量
            UILabel *taretLabel=[[UILabel alloc]initWithFrame:CGRectMake(15, 10, CGRectGetWidth(self.itemView3.frame)-30, 21)];
            taretLabel.text=[NSString stringWithFormat:@"%ld. %@",i+1,impressModel.impressDes];
            taretLabel.font=[UIFont systemFontOfSize:14];
            taretLabel.numberOfLines=0;
            taretLabel.textColor=[UIColor colorWithHexString:@"#676767"];
            // 设置文字属性 要和label的一致
            NSDictionary *attrs = @{NSFontAttributeName : taretLabel.font};
            CGSize maxSize = CGSizeMake(CGRectGetWidth(self.itemView2.frame)-30, MAXFLOAT);
            // 计算文字占据的高度
            CGSize size = [impressModel.impressDes boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
            // 设置label尺寸
            if (i==0) {
                taretLabel.frame = CGRectMake(0, 10, CGRectGetWidth(impressView.frame), size.height);
            } else {
                taretLabel.frame = CGRectMake(0, 0, CGRectGetWidth(impressView.frame), size.height);
            }
            
            
            [impressView addSubview:taretLabel];
            
            
            NSMutableArray *temaImageArray = impressModel.temaImageArray;
            
            
            //每行列数
            NSInteger rank = 3;
            //每列间距
            CGFloat rankMargin = 10;
            //每行间距
            CGFloat rowMargin = 10;
            //Item索引 ->根据需求改变索引
            NSUInteger index = 0;
            
            //每个Item宽高
            CGFloat W = (CGRectGetWidth(impressView.frame)-(rank-1)*rankMargin)/rank;
            CGFloat H = W;
            
            NSInteger itemImpressViewHeight = 0;
            if (temaImageArray==nil) {
                index=0;
            } else if(temaImageArray.count>=6){
                index = 6;
                itemImpressViewHeight+=H*2+15;
            }else  if(temaImageArray.count<=3){
                index = temaImageArray.count;
                itemImpressViewHeight+=H;
            }else{
                index = temaImageArray.count;
                itemImpressViewHeight+=(H*2+10);
            }
            
            //添加图片开始
            
            UIView *imageUiview = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(taretLabel.frame)+10, CGRectGetWidth(self.itemView3.frame), itemImpressViewHeight)];
            
            
            
            for (int i = 0 ; i< index; i++) {
                //Item X轴
                CGFloat X = (i % rank) * (W + rankMargin);
                //Item Y轴
                NSUInteger Y = (i / rank) * (H +rowMargin);
                //Item top
                CGFloat top = 0;
                UIView *speedView = [[UIView alloc] init];
                speedView.backgroundColor = [UIColor clearColor];
                speedView.frame = CGRectMake(X, Y+top, W, H);
                
                UIImageView *imageView =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, W, H)];
                [speedView addSubview:imageView];
                
                //[SINACircularBeadUtils makeCircularView:imageView];
                [imageView sd_setImageWithURL:[NSURL URLWithString:temaImageArray[i]]];
                [imageUiview addSubview:speedView];
            }
            
            [impressView addSubview:imageUiview];
            
            //添加图片结束
            
            NSString *time =impressModel.impressTime;
            
            time = [SINACalendarUtils getDateStringWithTimeStr:time andFormat:@"yyyy年MM月dd日 HH:mm"];
            
            UILabel *timeLabel=[[UILabel alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(imageUiview.frame)+10,CGRectGetWidth(impressView.frame), 21)];
            timeLabel.text=[NSString stringWithFormat:@"%@",time];
            timeLabel.font=[UIFont systemFontOfSize:14];
            timeLabel.numberOfLines=0;
            timeLabel.textColor=[UIColor colorWithHexString:@"#878787"];
            
            [impressView addSubview:timeLabel];
            
            UILabel *deleteLabel=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.itemView3.frame)/2, CGRectGetMaxY(imageUiview.frame)+10,CGRectGetWidth(self.itemView3.frame)-30, 21)];
            deleteLabel.text=@"删除";
            deleteLabel.font=[UIFont systemFontOfSize:14];
            deleteLabel.numberOfLines=0;
            deleteLabel.textColor=[UIColor colorWithHexString:@"#878787"];
            
            deleteLabel.tag=100+i;
            
            
            
            if ([self.model.itemStatue integerValue]==1||[self.model.itemStatue integerValue]==0) {
                
                
                [impressView addSubview:deleteLabel];
                
                [[SINAUiviewClickLiserner alloc]viewClickFunction:deleteLabel andWithBlick:^(UIView *view) {
                    if ([view isKindOfClass:[UILabel class]]) {
                        NSInteger tag =view.tag-100;
                        SINAStuActivityImpressModel  *model =self.teamImpressionArray[tag];
                        WISAlertView *alertView =  [[WISAlertView alloc]initWithTitle:@"提示" message:@"确定删除 ？" sureBtn:@"确定" cancleBtn:@"取消"];
                        alertView.resultIndex = ^(NSInteger index) {
                            [self deleteActiviImpressFunction:model andWithPos:tag];
                        };
                        [alertView showXLAlertView];
                    }
                }];
                
            }
            
            
            
            
            if(i!=self.teamImpressionArray.count-1){
                UIView *diviView = [[UIView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(deleteLabel.frame)+15, CGRectGetWidth(impressView.frame), 1)];
                diviView.backgroundColor=[UIColor colorWithHexString:@"#eeeeee"];
                [impressView addSubview:diviView];
                impressView.frame=CGRectMake(0, total+8, CGRectGetWidth(self.itemView3.frame)-30, CGRectGetMaxY(diviView.frame)+10);
                [self.impressionUiview addSubview:impressView];
                total=CGRectGetMaxY(impressView.frame)+2;
                
            }else{
                impressView.frame=CGRectMake(0, total+8, CGRectGetWidth(self.itemView3.frame)-30, CGRectGetMaxY(deleteLabel.frame)+15);
                [self.impressionUiview addSubview:impressView];
                total=CGRectGetMaxY(impressView.frame)+2;
                
            }
            
        }
        self.impressionUiview.frame =CGRectMake(15,CGRectGetMaxY(self.diviView3.frame),CGRectGetWidth(self.itemView3.frame)-30, total);
        self.itemView3.frame = CGRectMake(15, CGRectGetMaxY(self.itemView2.frame)+15, mainW-30, total+40);
    }else{
        self.diviView3.hidden=YES;
        self.impressionUiview.frame =CGRectMake(15,CGRectGetMaxY(self.diviView3.frame),CGRectGetWidth(self.itemView3.frame)-30,0);
        self.itemView3.frame = CGRectMake(15, CGRectGetMaxY(self.itemView2.frame)+15, mainW-30, 46);
    }
    self.scrollView.contentSize=CGSizeMake(mainW, CGRectGetMaxY(self.itemView3.frame)+15);
    
}

//删除随笔
-(void) deleteActiviImpressFunction:(SINAStuActivityImpressModel*) model andWithPos:(NSInteger) pos{

    [self startLoading];
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    
    [dict setObject:model.impressUid forKey:@"uuid"];
    NSMutableDictionary *dict2 = [[NSMutableDictionary alloc]init];
    [dict2 setObject:[SINAHttpRequestUtils encodeDicParams:dict] forKey:@"sj"];
    WeakSelf;
    [CXAFNHttpRequestUtils httpPostRequest:[NSString stringWithFormat:@"%@%@",baseUrl,userActivityImpressDeleteUrl] withParamters:dict2 success:^(id responseData) {
        
        [weakSelf stopLoading];
        
        NSString *code = responseData[@"code"];
        
        if ([code isEqualToString:@"1000"]) {
            //添加成功
//            self.loadingView.text=@"删除成功";
            [YPXToastView showToastViewWithText:@"删除成功" andDuration:2 andCorner:5 andParentView:weakSelf.view];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.2*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [weakSelf loadingTarGetDataFunction];
            });
        }else{
            NSString *message =responseData[@"message"];
            WISAlertView *alertView =  [[WISAlertView alloc]initWithTitle:@"提示" message:message sureBtn:@"确定" cancleBtn:nil];
            [alertView showXLAlertView];
        }
        
    } failure:^(NSError *error, NSInteger errCode) {
        DLog(@"加载 失败  %@ ",error.debugDescription);
        [weakSelf stopLoading];
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"删除失败 网络异常" sureBtn:@"确定" cancleBtn:nil];
        [alertView showXLAlertView];
    }];
}

//组队完成功能提交
- (IBAction)finishActivityFunction:(UIButton *)sender {
    
    if (self.teamTargetArray.count>0) {
        BOOL isTargetFinish=YES;
        for (NSInteger i=0; i<self.teamTargetArray.count; i++) {
            
            SINAStuActivityTargetModel * targetModel=self.teamTargetArray[i];
            if (targetModel.endTime==0) {
                isTargetFinish = NO;
            }
        }
        if (isTargetFinish) {
            [self checkCommitDataFunction];
        } else {
            WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"还有未完成的小目标" sureBtn:@"确定" cancleBtn:nil];
            [alertView showXLAlertView];
        }
    }else{
        [self checkCommitDataFunction];
    }
}

//提交数据
-(void) checkCommitDataFunction{
    
    //设置完成按钮点击事件
    NSInteger activityStartTime = [self.model.activityStartTime integerValue];
    NSInteger activityEndTime = [self.model.activityStopTime integerValue];
    //当前时间
    NSInteger currentTime = [[SINACalendarUtils currentTimeStr] integerValue];
    
    if (currentTime<activityStartTime) {
        DLog(@"活动未开始");
        [YPXToastView showToastViewWithText:@"活动未开始" andDuration:1 andParentView:self.itemView1];
    }else if(currentTime>=activityStartTime&&currentTime<=(activityEndTime)){
        //活动开始 以及结束后的半小时
        DLog(@"活动中");
        [YPXToastView showToastViewWithText:@"活动中" andDuration:1 andParentView:self.itemView1];
    }else if(currentTime>=activityEndTime&&currentTime<=(activityEndTime+30*60*1000)){
        //活动开始 以及结束后的半小时
        DLog(@"完成");
        [self commitDataFunction];
    }else{
        //超时
        [YPXToastView showToastViewWithText:@"活动已超时" andDuration:1 andParentView:self.itemView1];
        DLog(@"超时");
    }
    
}

-(void) commitDataFunction{
    
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    NSMutableDictionary *dict2 = [[NSMutableDictionary alloc]init];
    
    [dict setObject:self.model.activityUid forKey:@"uuid"];
    
    [dict2 setObject:[SINAHttpRequestUtils encodeDicParams:dict] forKey:@"sj"];
    
    WeakSelf;
    [CXAFNHttpRequestUtils httpPostRequest:[NSString stringWithFormat:@"%@%@",baseUrl,userStuHomeFinishActivityUrl] withParamters:dict2 success:^(id responseData) {
        
        NSString *code = responseData[@"code"];
        if ([code isEqualToString:@"1000"]) {
            WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"提交成功" sureBtn:@"去查看"
                                                                 cancleBtn:nil];
            alertView.resultIndex = ^(NSInteger index) {
                
                weakSelf.model.itemStatue=[NSString stringWithFormat:@"2"];
                weakSelf.activityFinishButton.hidden = YES;
                
                //创建通知
                NSNotification *notification =[NSNotification notificationWithName:@"stuActivityAddFindOther" object:nil userInfo:dict];
                // 通过 通知中心 发送 通知
                [[NSNotificationCenter defaultCenter] postNotification:notification];
                
                [weakSelf.navigationController popViewControllerAnimated:YES];
                
            };
            [alertView showXLAlertView];
        }else{
            NSString *message =responseData[@"message"];
            WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:message sureBtn:@"确定"
                                                                 cancleBtn:nil];
            [alertView showXLAlertView];
        }
        
        
        
    } failure:^(NSError *error, NSInteger errCode) {
        DLog(@"加载 失败  %@ ",error.debugDescription);
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"网络异常" sureBtn:@"确定"
                                                             cancleBtn:nil];
        [alertView showXLAlertView];
    }];
}

@end
